package com.reider.log4AS3.appenders
{
	import com.reider.log4AS3.core.log_internal;
	import com.reider.log4AS3.dataholder.Log;
	import com.reider.log4AS3.layouts.Layout;
	
	import flash.utils.getQualifiedClassName;
	
	/**
	 * This is an abstract class you have to extend it to create custom appenders.
	 */
	public class Appender
	{
		private var _name:String
		private var _layout:Layout
		
		/**
		 * The layout
		 */
		
		public function get layout():Layout
		{
			return _layout;
		}

		public function set layout(value:Layout):void
		{
			_layout = value;
		}

		/**
		 * Returns the name of the appender.
		 */
		
		public function get name():String
		{
			return _name;
		}
		
		/**
		 * You should not use the constructor to create this class.
		 * 
		 * @param name The name of the appende.
		 */
		
		public function Appender(name:String)
		{
			if(this["constructor"] == Appender)
				throw new ArgumentError("Appender is an abstract class it must be extended");
			
			if(!name) 
				throw new ArgumentError("Appender must have a valid name");
			
			_name = name;
		}
		
		/**
		 * @private
		 */
		
		public final function addLog(log:Log):void
		{
			if(layout){
				var formattedText:String = layout.format( log )
				log.log_internal::formatedText = formattedText
				print( log )
			}else{
				print( log )
			}
		}
		
		/**
		 * This is an abstract method you must override it in the extended class.
		 * 
		 * @param log The log instance containing the logged values.
		 */
		
		public function print(log:Log):void
		{
			throw new Error("print is an abstract method you must override it")
		}

	}
}